package com.cci.kangdao.dao;


import com.cci.kangdao.dao.model.RolesNotifyRecordT;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;
import java.util.Map;

@MapperScan
public interface RolesNotifyRecordTDao {

    int insertSelective(RolesNotifyRecordT record);

    /**
     * 获取用户角色申请消息列表
     * @param userId 用户id'
     * @return 消息列表
     */
   /* @Select("select notify.ID as id,notify.SendTime as sendTime,notify.IsRead as isRead,orders.ID as orderId,orders.UserId as userId,orders.RoleName as roleName,\n" +
            "                   orders.OrganizationName as organizationName,orders.HandlerId as handlerId,orders.RefuseMessage as message,notify.Status as status\n" +
            "            from RolesOrder_T orders inner join RolesNotifyRecord_T notify on orders.Flag = 0 and notify.Flag = 0 and notify.OrderId = orders.ID\n" +
            "            inner join User_T users on users.Flag = 0 and users.ID = orders.UserId\n" +
            "            where notify.ReceiverId = #{userId} order by isRead asc,notify.SendTime desc")*/
    List<Map<String, Object>> selectListByUserId(Map<String, Object> map);

    /**
     * 已读消息
     * @param id
     * @return
     */
    @Update("update RolesNotifyRecord_T set IsRead = 1 where Flag = 0 and ID = #{id}")
    int readNotifyById(Long id);

    @Select("select count(*) from RolesNotifyRecord_T record\n" +
            "      inner join RolesOrder_T orders on orders.Flag = 0  and record.OrderId = orders.ID\n" +
            "      inner join User_T users on users.Flag = 0 and orders.UserId = users.ID\n" +
            "      where record.IsRead=0 and record.ReceiverId= #{receiverId}")
    int unReadNotifyCount(Long receiverId);

    @Update("update RolesNotifyRecord_T set IsRead = 1 ,Updator = #{updator}, UpdateTime = #{updateTime} where IsRead = 0 and ReceiverId = #{receiverId}")
    int updateNotifyReadStatusByReceiverId(@Param("receiverId") Long ReceiverId,@Param("updator") Long updator,@Param("updateTime") String updateTime);
}